<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","width":300,"display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="本篇介绍如何在Centos-7中基于Docker安装Jenkins。">
<meta property="og:type" content="article">
<meta property="og:title" content="基于Docker搭建Jenkins">
<meta property="og:url" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/index.html">
<meta property="og:site_name" content="履歴の本文">
<meta property="og:description" content="本篇介绍如何在Centos-7中基于Docker安装Jenkins。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/000.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/001.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/008.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/002.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/003.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/004.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/005.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/006.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/007.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/009.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/010.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/011.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/012.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/013.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/014.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/015.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/016.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/017.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/018.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/019.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/020.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/021.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/022.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/023.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/024.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/025.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/026.png">
<meta property="og:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/027.png">
<meta property="article:published_time" content="2020-11-10T08:22:33.000Z">
<meta property="article:modified_time" content="2020-11-23T09:09:39.969Z">
<meta property="article:author" content="モンキーDルフィ">
<meta property="article:tag" content="docker">
<meta property="article:tag" content="devops">
<meta property="article:tag" content="jenkins">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/000.png">

<link rel="canonical" href="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>基于Docker搭建Jenkins | 履歴の本文</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">履歴の本文</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">天下风云出我辈，一入代码岁月催</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">15</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">7</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">18</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lufei.jpg">
      <meta itemprop="name" content="モンキーDルフィ">
      <meta itemprop="description" content="海贼王におれはなる！">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="履歴の本文">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          基于Docker搭建Jenkins
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-11-10 16:22:33" itemprop="dateCreated datePublished" datetime="2020-11-10T16:22:33+08:00">2020-11-10</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/jenkins/" itemprop="url" rel="index"><span itemprop="name">jenkins</span></a>
                </span>
            </span>

          
            <span id="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/" class="post-meta-item leancloud_visitors" data-flag-title="基于Docker搭建Jenkins" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
              <span>4k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>本篇介绍如何在Centos-7中基于Docker安装Jenkins。</p>
<a id="more"></a>

<h2 id="Pull-Image"><a href="#Pull-Image" class="headerlink" title="Pull Image"></a>Pull Image</h2><p>拉取镜像</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ docker pull jenkins/jenkins</span><br></pre></td></tr></table></figure>

<h2 id="Create-Mount-Dir"><a href="#Create-Mount-Dir" class="headerlink" title="Create Mount Dir"></a>Create Mount Dir</h2><p>创建挂载目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir -p /var/jenkins_home</span><br></pre></td></tr></table></figure>

<h2 id="Create-And-Run-Jenkins-Container"><a href="#Create-And-Run-Jenkins-Container" class="headerlink" title="Create And Run Jenkins Container"></a>Create And Run Jenkins Container</h2><p>创建并启动容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$ docker run -u root --privileged=<span class="literal">true</span> \</span><br><span class="line">-d -p 8080:8080 -p 50000:50000 -p 8000:8000 \</span><br><span class="line">-v /var/run/docker.sock:/var/run/docker.sock \</span><br><span class="line">-v /var/lib/docker:/usr/bin/docker \</span><br><span class="line">-v /usr/<span class="built_in">local</span>/java/jdk1.8.0_271:/usr/<span class="built_in">local</span>/java/jdk1.8.0_271 \</span><br><span class="line">-v /usr/<span class="built_in">local</span>/maven/apache-maven-3.6.3:/usr/<span class="built_in">local</span>/maven \</span><br><span class="line">-v /usr/<span class="built_in">local</span>/maven/repo:/usr/<span class="built_in">local</span>/maven_repository \</span><br><span class="line">-v /usr/<span class="built_in">local</span>/app:/usr/<span class="built_in">local</span>/app \</span><br><span class="line">-v /var/jenkins_home:/var/jenkins_home \</span><br><span class="line">--name jenkins jenkins/jenkins</span><br></pre></td></tr></table></figure>
<p>其中，<code>/usr/local/app</code>是宿主机存放jar包和启动脚本<code>service.sh</code>的目录，因此需要与容器进行挂载。<code>8000</code>端口是SpringBoot项目端口，本篇中启动的应用进程是在jenkins容器内部，因此也需要通过宿主机的8000端口穿透进去才能访问。这里的<code>/usr/local/app</code>和<code>8000</code>端口算是笔者的一点小心思吧，是笔者踩了许多坑才跑通的。实际应用中是不可能这么玩的，大家当作学习docker和jenkins的一个案例就好了。</p>
<h2 id="Find-Initial-Admin-Password"><a href="#Find-Initial-Admin-Password" class="headerlink" title="Find Initial Admin Password"></a>Find Initial Admin Password</h2><p>获取初始管理员密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ cat /var/jenkins_home/secrets/initialAdminPassword </span><br></pre></td></tr></table></figure>

<h2 id="Visit-Jenkins"><a href="#Visit-Jenkins" class="headerlink" title="Visit Jenkins"></a>Visit Jenkins</h2><p>访问Jenkins，输入上一步获取的初始管理员密码<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/000.png" alt="000"><br>点击继续<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/001.png" alt="001"><br>安装推荐的插件，等待完成，不管安装成功或失败，继续<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/008.png" alt="008"><br>创建用户并按照提示走<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/002.png" alt="002"><br>开始你的Jenkins旅程吧<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/003.png" alt="003"></p>
<h2 id="Update-Site"><a href="#Update-Site" class="headerlink" title="Update Site"></a>Update Site</h2><p>更新插件源为中国源</p>
<h3 id="Manage-Plugins"><a href="#Manage-Plugins" class="headerlink" title="Manage Plugins"></a>Manage Plugins</h3><p>插件管理<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/004.png" alt="004"></p>
<h3 id="Advanced"><a href="#Advanced" class="headerlink" title="Advanced"></a>Advanced</h3><p>高级特性<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/005.png" alt="005"></p>
<h3 id="Update-Site-1"><a href="#Update-Site-1" class="headerlink" title="Update Site"></a>Update Site</h3><p>修改源<code>https://updates.jenkins.io/update-center.json</code>为<code>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</code><br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/006.png" alt="006"></p>
<h3 id="Modify-The-Configuration-File"><a href="#Modify-The-Configuration-File" class="headerlink" title="Modify The Configuration File"></a>Modify The Configuration File</h3><p>修改配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /var/jenkins_home/updates/</span><br><span class="line">$ sed -i <span class="string">&#x27;s#https://updates.jenkins.io/download#https://mirrors.tuna.tsinghua.edu.cn/jenkins#g&#x27;</span> default.json &amp;&amp; sed -i <span class="string">&#x27;s#http://www.google.com#https://www.baidu.com#g&#x27;</span> default.json</span><br></pre></td></tr></table></figure>

<h3 id="Restart-Jenkins"><a href="#Restart-Jenkins" class="headerlink" title="Restart Jenkins"></a>Restart Jenkins</h3><p>重启Jenkins</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ docker restart xxx</span><br></pre></td></tr></table></figure>
<p>Ok，插件源更换成功，之后再安装插件速度就很快了</p>
<h3 id="Install-Localization-Chinese-Simplified"><a href="#Install-Localization-Chinese-Simplified" class="headerlink" title="Install Localization: Chinese (Simplified)"></a>Install <code>Localization: Chinese (Simplified)</code></h3><p>安装中文显示插件<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/007.png" alt="007"><br>安装完毕后重启Jenkins即可。</p>
<h2 id="Global-Tool-Configuration"><a href="#Global-Tool-Configuration" class="headerlink" title="Global Tool Configuration"></a>Global Tool Configuration</h2><p>全局工具配置<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/009.png" alt="009"><br>首先配置Maven：<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/010.png" alt="010"><br>然后拉到最下边，新增Maven，去掉自动安装的勾选，手动配置：<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/011.png" alt="011"><br>点击保存</p>
<h2 id="Create-Job"><a href="#Create-Job" class="headerlink" title="Create Job"></a>Create Job</h2><p>创建任务<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/012.png" alt="012"><br>选择自由风格，点击确定<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/013.png" alt="013"><br>源码管理，填写代码地址，然后添加用户（注意此用户为git远程仓库的用户，比如gitlab、gitee或者github）<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/014.png" alt="014"><br>选中添加的用户<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/015.png" alt="015"><br>构建，添加构建步骤，调用顶层Maven目标<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/016.png" alt="016"><br>选择我们增加的Maven<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/017.png" alt="017"><br>点击保存</p>
<h2 id="Execute-The-Job"><a href="#Execute-The-Job" class="headerlink" title="Execute The Job"></a>Execute The Job</h2><p>立即构建<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/018.png" alt="018"><br>点击具体任务<code>#1</code>，控制台输出，查看构建日志<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/019.png" alt="019"><br>这里可以看到，先是从git远程仓库拉代码，然后执行maven打包<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/020.png" alt="020"><br>这里打完包之后，具体jar包存放在什么地方呢？实际上在jenkins的工作空间（/var/jenkins_home/workspace/jenkins-demo/target）下：<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/021.png" alt="021"></p>
<h2 id="Add-Build-Step"><a href="#Add-Build-Step" class="headerlink" title="Add Build Step"></a>Add Build Step</h2><p>增加构建步骤。通常生产中打完包之后会将jar包scp（ssh传输）到应用服务器上，并将其启动。这里我们在一台机器上模拟此过程，就进行就不ssh了。<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/022.png" alt="022"><br>Shell的内容如下：<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/023.png" alt="023"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># 告诉Jenkins这段脚本执行完之后，其中启动的进程不被杀掉。否则service.sh脚本中启动的java进程进会被杀掉。</span></span><br><span class="line">BUILD_ID=dontKillMe</span><br><span class="line"><span class="comment"># 切换到应用目录</span></span><br><span class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/app</span><br><span class="line"><span class="comment"># 执行预存的脚本，首先停止服务</span></span><br><span class="line">sh service.sh stop</span><br><span class="line"><span class="comment"># 删除原有的jar包</span></span><br><span class="line">rm -f *.jar</span><br><span class="line"><span class="comment"># 切换到Jenkins生成jar包的目录</span></span><br><span class="line"><span class="built_in">cd</span> <span class="variable">$&#123;WORKSPACE&#125;</span>/target</span><br><span class="line"><span class="comment"># 将此处的jar包移动到应用目录</span></span><br><span class="line">mv *.jar /usr/<span class="built_in">local</span>/app/</span><br><span class="line"><span class="comment"># 切换到应用目录</span></span><br><span class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/app</span><br><span class="line"><span class="comment"># 脚本启动服务</span></span><br><span class="line">sh service.sh start</span><br></pre></td></tr></table></figure>
<p>点击保存。然后我们看下<code>/usr/local/app</code>下的内容：<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/024.png" alt="024"><br>service.sh脚本的内容如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#-----------------------------------------------------------</span></span><br><span class="line"><span class="comment"># 本Shell用于启动、停止、重启SpringBoot服务以及查看服务状态</span></span><br><span class="line"><span class="comment">#-----------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line">APP_NAME=`ls |grep .jar$ |head -n1`</span><br><span class="line"></span><br><span class="line"><span class="comment">#使用说明，用来提示输入参数</span></span><br><span class="line"><span class="function"><span class="title">usage</span></span>() &#123;</span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;Usage: sh <span class="variable">$0</span> [start|stop|restart|status]&quot;</span></span><br><span class="line">    <span class="built_in">exit</span> 1</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#检查程序是否在运行</span></span><br><span class="line"><span class="function"><span class="title">is_exist</span></span>() &#123;</span><br><span class="line">  pid=`ps -ef |grep java|grep <span class="variable">$&#123;APP_NAME&#125;</span>|awk <span class="string">&#x27;&#123;print $2&#125;&#x27;</span>`</span><br><span class="line">  <span class="comment">#如果不存在返回1，存在返回0</span></span><br><span class="line">  <span class="keyword">if</span> [ -z <span class="variable">$&#123;pid&#125;</span> ]; <span class="keyword">then</span></span><br><span class="line">   <span class="built_in">return</span> 1</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">    <span class="built_in">return</span> 0</span><br><span class="line">  <span class="keyword">fi</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#启动方法</span></span><br><span class="line"><span class="function"><span class="title">start</span></span>() &#123;</span><br><span class="line">  <span class="built_in">echo</span> <span class="string">&quot;即将启动<span class="variable">$&#123;APP_NAME&#125;</span>......&quot;</span></span><br><span class="line">  is_exist</span><br><span class="line">  <span class="keyword">if</span> [ $? -eq <span class="string">&quot;0&quot;</span> ]; <span class="keyword">then</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$&#123;APP_NAME&#125;</span>已运行,pid=<span class="variable">$&#123;pid&#125;</span>.&quot;</span></span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$&#123;APP_NAME&#125;</span>启动中......&quot;</span></span><br><span class="line">    nohup java -jar ./<span class="variable">$&#123;APP_NAME&#125;</span> &gt; out.log 2&gt;&amp;1 &amp;</span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$&#123;APP_NAME&#125;</span>启动完毕！&quot;</span></span><br><span class="line">  <span class="keyword">fi</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#停止方法</span></span><br><span class="line"><span class="function"><span class="title">stop</span></span>() &#123;</span><br><span class="line">  <span class="built_in">echo</span> <span class="string">&quot;即将停止<span class="variable">$&#123;APP_NAME&#125;</span>......&quot;</span></span><br><span class="line">  is_exist</span><br><span class="line">  <span class="keyword">if</span> [ $? -eq <span class="string">&quot;0&quot;</span> ]; <span class="keyword">then</span></span><br><span class="line">    <span class="built_in">kill</span> -9 <span class="variable">$pid</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$&#123;APP_NAME&#125;</span>停止完毕！&quot;</span></span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$&#123;APP_NAME&#125;</span>未运行,无需停止......&quot;</span></span><br><span class="line">  <span class="keyword">fi</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#输出运行状态</span></span><br><span class="line"><span class="function"><span class="title">status</span></span>() &#123;</span><br><span class="line">  is_exist</span><br><span class="line">  <span class="keyword">if</span> [ $? -eq <span class="string">&quot;0&quot;</span> ]; <span class="keyword">then</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$&#123;APP_NAME&#125;</span> is running. Pid is <span class="variable">$&#123;pid&#125;</span>.&quot;</span></span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$&#123;APP_NAME&#125;</span> is not running.&quot;</span></span><br><span class="line">  <span class="keyword">fi</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#重启</span></span><br><span class="line"><span class="function"><span class="title">restart</span></span>()&#123;</span><br><span class="line">  stop</span><br><span class="line">  start</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#根据输入参数，选择执行对应方法，不输入则执行使用说明</span></span><br><span class="line"><span class="keyword">case</span> <span class="string">&quot;<span class="variable">$1</span>&quot;</span> <span class="keyword">in</span></span><br><span class="line">  <span class="string">&quot;start&quot;</span>)</span><br><span class="line">    start</span><br><span class="line">    ;;</span><br><span class="line">  <span class="string">&quot;stop&quot;</span>)</span><br><span class="line">    stop</span><br><span class="line">    ;;</span><br><span class="line">  <span class="string">&quot;status&quot;</span>)</span><br><span class="line">    status</span><br><span class="line">    ;;</span><br><span class="line">  <span class="string">&quot;restart&quot;</span>)</span><br><span class="line">    restart</span><br><span class="line">    ;;</span><br><span class="line">  *)</span><br><span class="line">    usage</span><br><span class="line">    ;;</span><br><span class="line"><span class="keyword">esac</span></span><br></pre></td></tr></table></figure>
<p>再次执行任务，可以看到脚本执行的日志：<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/025.png" alt="025"><br>我们查一下java进程，发现jar包已启动：<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/026.png" alt="026"><br>然后访问一下项目：<br><img src="/2020/11/10/%E5%9F%BA%E4%BA%8EDocker%E6%90%AD%E5%BB%BAJenkins/027.png" alt="027"></p>

    </div>

    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/docker/" rel="tag"><i class="fa fa-tag"></i> docker</a>
              <a href="/tags/devops/" rel="tag"><i class="fa fa-tag"></i> devops</a>
              <a href="/tags/jenkins/" rel="tag"><i class="fa fa-tag"></i> jenkins</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/11/10/CentOS-7%E5%AE%89%E8%A3%85Docker/" rel="prev" title="CentOS-7安装Docker">
      <i class="fa fa-chevron-left"></i> CentOS-7安装Docker
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/11/11/CentOS-7%E5%AE%89%E8%A3%85JDK1.8/" rel="next" title="CentOS-7安装jdk1.8">
      CentOS-7安装jdk1.8 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Pull-Image"><span class="nav-number">1.</span> <span class="nav-text">Pull Image</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Create-Mount-Dir"><span class="nav-number">2.</span> <span class="nav-text">Create Mount Dir</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Create-And-Run-Jenkins-Container"><span class="nav-number">3.</span> <span class="nav-text">Create And Run Jenkins Container</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Find-Initial-Admin-Password"><span class="nav-number">4.</span> <span class="nav-text">Find Initial Admin Password</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Visit-Jenkins"><span class="nav-number">5.</span> <span class="nav-text">Visit Jenkins</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Update-Site"><span class="nav-number">6.</span> <span class="nav-text">Update Site</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Manage-Plugins"><span class="nav-number">6.1.</span> <span class="nav-text">Manage Plugins</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Advanced"><span class="nav-number">6.2.</span> <span class="nav-text">Advanced</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Update-Site-1"><span class="nav-number">6.3.</span> <span class="nav-text">Update Site</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Modify-The-Configuration-File"><span class="nav-number">6.4.</span> <span class="nav-text">Modify The Configuration File</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Restart-Jenkins"><span class="nav-number">6.5.</span> <span class="nav-text">Restart Jenkins</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Install-Localization-Chinese-Simplified"><span class="nav-number">6.6.</span> <span class="nav-text">Install Localization: Chinese (Simplified)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Global-Tool-Configuration"><span class="nav-number">7.</span> <span class="nav-text">Global Tool Configuration</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Create-Job"><span class="nav-number">8.</span> <span class="nav-text">Create Job</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Execute-The-Job"><span class="nav-number">9.</span> <span class="nav-text">Execute The Job</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Add-Build-Step"><span class="nav-number">10.</span> <span class="nav-text">Add Build Step</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="モンキーDルフィ"
      src="/images/lufei.jpg">
  <p class="site-author-name" itemprop="name">モンキーDルフィ</p>
  <div class="site-description" itemprop="description">海贼王におれはなる！</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">18</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>
      <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=174419&auto=1&height=66"></iframe>
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">モンキーDルフィ</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">66k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">1:01</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'rp9eaSA9Y6vBrWUdOf5ukLYs-gzGzoHsz',
      appKey     : 'SIkchhA5LF0xmXpXKtbkA65H',
      placeholder: "欢迎大家前来交流~",
      avatar     : '/images/lufei.jpg',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
